﻿//==============================================================================
// File: RegexTextBox.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arek Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfRcp.Controls
{
    public class RegexTextBox : TextBox
    {
        public static readonly DependencyProperty RegularExpressionProperty =
            DependencyProperty.Register("RegularExpression", typeof(string),
                                        typeof(RegexTextBox), new UIPropertyMetadata(string.Empty, OnRegularExpressionChanged));

        protected Regex CachedRegex { get; private set; }

        static RegexTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RegexTextBox), new FrameworkPropertyMetadata(typeof(RegexTextBox)));
        }

        public RegexTextBox()
        {
            AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(OnPastingHandler));
        }

        public string RegularExpression
        {
            get { return (string)GetValue(RegularExpressionProperty); }
            set { SetValue(RegularExpressionProperty, value); }
        }

        private static void OnRegularExpressionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var maskedTextBox = (RegexTextBox) d;

            if (e.OldValue != e.NewValue)
            {
                maskedTextBox.CachedRegex = new Regex((string) e.NewValue);
            }
        }

        private void OnPastingHandler(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                var text = (string) e.DataObject.GetData(typeof(string));
                
                if (!IsTextValid(text))
                    e.CancelCommand();
            }
            else
                e.CancelCommand();
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            e.Handled = !IsTextValid(e.Text);
            
            base.OnPreviewTextInput(e);
        }

        private bool IsTextValid(string text)
        {
            Match match = CachedRegex.Match(Text + text);

            return match.Success;
        }
    }
}